﻿function onAddBtn() {
    var $ = layui.jquery;
    var layer = layui.layer; // 添加layui弹出层模块
    var laydate = layui.laydate;
    //渲染
    laydate.render({
        elem: '#storageDate'
    });

    layer.open({
        type: 1,
        title: "新增网格类型",
        area: ['40%', '40%'],
        shade: 0,
        shadeClose: true,
        maxmin: true,
        anim: 0,
        content: $("#add-main"),
    });
}
//第一个参数为操作内容，例如：登录了系统，修改/删除/添加/查询了日志表（或其他表，每次调用根据进行的操作写）
//第二个参数为修改专用，将修改前的参数转为json字符串格式传入
//第三个参数是将添加或删除的数据转为json传入，若是修改方法则把修改后的数据转为json字符串传入
function AddLogInfo(OperationalContext, PreOperation, AfterOperation) {
    const data = {
        olContext: OperationalContext,
        PreOperation: PreOperation,
        AfterOperation: AfterOperation
    };
    // alert(JSON.stringify(data));
    $.ajax({
        url: "http://8.140.245.229:8000/WorkBench/api/Log/AddLog",
        type: 'post',
        dataType: 'json',
        headers: {
            'Authorization': "Bearer " + localStorage["token"] + ""
        },
        contentType: 'application/json', // 添加正确的 Content-Type 头部
        data: JSON.stringify(data),
        success: function (res) {
            // alert(res)
            getListData(1);
            console.log(res)
        }
    })
}

layui.use(['table', 'laypage', 'layer', 'form'], function () {
    var tree = layui.tree;
    var table = layui.table;
    var laypage = layui.laypage;
    var $ = layui.jquery;
    var form = layui.form;
    var layer = layui.layer; // 添加layui弹出层模块

    var checkedIds = []; // 存储选中节点的 id
    var checkedIdsAll = [];

    var pageSize = 15; // 每页显示条数

    // 模拟数据
    var data = [{ title: '呼兰区', id: 1, spread: true, children: [{ title: '呼兰街道', id: 1000, spread: true, children: [{ title: '东风社区', id: 10001 }, { title: '文化社区', id: 10002 }] }, { title: '兰河街道', id: 1001 }, { title: '利民街道', id: 1002 }, { title: '康金街道', id: 1002 }, { title: '双井街道', id: 1002 }, { title: '长岭街道', id: 1002 }] }];
    // 渲染
    tree.render({
        elem: '#ID-tree-demo',
        data: data,
        showCheckbox: false,  // 是否显示复选框
        onlyIconControl: true,  // 是否仅允许节点左侧图标控制展开收缩
        id: 'demo-id-1',
        isJump: true, // 是否允许点击节点时弹出新窗口跳转
    });
    
    //获取列表  显示
    function getListData(pageIndex) {
        //获取查询条件
        var gridname = $('#gridname').val();
        var owningregion = $('#owningregion').val();
        //ajax后台交互
        $.ajax({
            url: ' http://8.140.245.229:8000/GridRed/api/FoundationGrid/FoundationGridShow',
            type: 'get',
            dataType: 'json',
            headers: {
                'Authorization': "Bearer " + localStorage["token"] + ""
            },
            //参数
            data: {
                pageIndex: pageIndex,
                pageSize: pageSize,
                gridname: gridname,
                owningregion: owningregion
            },
            success: function (res) {
                //渲染表格数据
                layui.use(['table', 'laypage'], function () {
                    var table = layui.table;
                    var laypage = layui.laypage;

                    table.render({
                        //挂载的表格id
                        elem: '#ID-table-demo-setRowChecked',
                        cols: [[
                            { type: 'checkbox', unresize: true},
                            { type: 'numbers', title: '序号'},
                            { field: 'grid_numbering', title: '网格编号' },
                            { field: 'gridname', title: '网格名称' },
                            { field: 'owningregion', title: '所属区域'},
                            { field: 'subordinatelevel', title: '所属层级' },
                            { title: '操作', templet: '#barDemo', fixed: 'right', unresize: true },
                        ]],
                        toolbar: '#demo-toolbar-setRowChecked',
                        initSort: { // 设置初始排序
                            field: 'id', // 字段名
                            type: 'desc' // 倒序
                        },

                        data: res.list
                    });

                    //分页
                    laypage.render({
                        elem: 'pagesbox', // 元素 id
                        count: res.totalcount, // 数据总数
                        layout: ['count', 'prev', 'page', 'next', 'limit', 'refresh', 'skip'], // 功能布局
                    });
                });
                var obj = {};
                // 提交事件
                form.on('submit(demo2)', function (data) {
                    var field = data.field;
                    var index = parent.layer.getFrameIndex(window.name);


                    obj.grid_numbering = field.grid_numbering;
                    obj.gridname = field.gridname;
                    obj.owningregion = field.owningregion;
                    obj.subordinatelevel = field.subordinatelevel;
                    $.ajax({
                        url: "http://8.140.245.229:8000/Grid/api/FoundationGrid/FoundationGridAdd",
                        type: 'post',
                        data: JSON.stringify(obj),
                        contentType: "application/json",
                        success: res => {
                            if (res > 0) {
                                layer.alert('添加成功');
                                parent.layer.close(index);//关闭当前弹窗页面
                                location.href = "/GridManagement/FoundationGridShow";
                            }
                            else if (res==-1){
                                layer.msg('网格编码不能重复：');
                            }
                            else {
                                layer.msg('添加失败：');
                            }
                        },
                        error: function () {
                            // 请求失败的处理
                            layer.msg('请求失败，请重试');
                        }
                    });
                    // 阻止表单默认提交
                    return false;
                });
                $('#searchBtn').on('click', function () {
                    getListData(1);
                });
            }
        });
    }

    $(function () {
        // 初始化加载数据
        getListData(1);
    })
});
